//
//  XLModifyPSPresenter.m
//  industry
//
//  Created by xl on 2020/12/10.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLModifyPSPresenter.h"
#import "XLModifyPSPresenterProtocol.h"
#import "XLModifyPSViewProtocol.h"
#import "XLModifyPSVC.h"
#import "XLAES128.h"
#import "XLModifyPSInteractorProtocol.h"

@interface XLModifyPSPresenter ()<XLModifyPSPresenterProtocol>

@end
@implementation XLModifyPSPresenter

- (void)dealloc {
    XLLog(@"XLModifyPSPresenter dealloc");
}

#pragma mark - XLModifyPSPresenterProtocol
//获取AESkey执行对应操作
- (void)requestFetchAESKey:(NSString *)password {
    XLModifyPSVC *vc = (XLModifyPSVC *)self.baseController;
    
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    XLSend(self.baseController.cView, XLModifyPSViewProtocol, showLoadingAnimalWithTitle:@"");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:XLFetchAESKeyUrl params:nil isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
        NSString *aesKey = [dict objectForKey:@"data"];
        if (vc.fromType == XLModifyPSVCFromTypeNext) {
            [strongSelf requestCheckOldPassword:password aesKey:aesKey];
        } else {
            [strongSelf requestUpdateNewPassWord:password aesKey:aesKey];
        }

    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
    }];

}


- (void)requestCheckOldPassword:(NSString *)oldPassword
                         aesKey:(NSString *)aesKey {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    NSString *aesStr = [XLAES128 encryptStringWithString:oldPassword andKey:aesKey];
    if (!ISEmptyString(aesStr)) {
        [params setValue:aesStr forKey:@"oldPassword"];
    }
    
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    NSString *url = XL_UserInfoData.isStaff ? XLCheckStaffOldPasswordUrl:XLCheckUnStaffOldPasswordUrl;
    
    XLSend(self.baseController.cView, XLModifyPSViewProtocol, showLoadingAnimalWithTitle:@"校验中...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:url params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
         STRONGSELF
        NSArray *temArr;
        id data = [dict objectForKey:@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            temArr = (NSArray *)data;
        }
        XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
        XLSend(strongSelf.baseController.interactor, XLModifyPSInteractorProtocol, gotoModifyVC:temArr);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
    }];
}


- (void)requestUpdateNewPassWord:(NSString *)password
                          aesKey:(NSString *)aesKey {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    NSString *aesStr = [XLAES128 encryptStringWithString:password andKey:aesKey];
    if (!ISEmptyString(aesStr)) {
        [params setValue:aesStr forKey:@"newPassword"];
        [params setValue:aesStr forKey:@"reNewPassword"];
    }
    
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    NSString *url = XL_UserInfoData.isStaff ? XLUpdateStaffPasswordUrl:XLUpdateUnStaffPasswordUrl;
    
    XLSend(self.baseController.cView, XLModifyPSViewProtocol, showLoadingAnimalWithTitle:@"校验中...");
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypePOST url:url params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
        
        [XLToast showSuccessMessage:@"更换密码成功"];
        //返回上一次
        XLSend(strongSelf.baseController.interactor, XLModifyPSInteractorProtocol, gotBackLastVC);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
         STRONGSELF
         XLSend(strongSelf.baseController.cView, XLModifyPSViewProtocol, hideLoadingAnimal);
    }];
    
}
@end
